package org.jeecg.modules.app.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.jeecg.modules.app.utils.encrypt.AesUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 返回的JSON消息体
 */
@Component
public class JsonUtil {
	private static final Logger logger = LoggerFactory.getLogger(JsonUtil.class);

	private static boolean devFlag;

	@Value("${app.devFlag}")
	public void setDevFlag(boolean devFlag) {
		JsonUtil.devFlag = devFlag;
	}

	/**
	 * 成功的JSON
	 * @param data 返回结果
	 * @return
	 */
	public static JSONObject successJson(Object data) {

		Map map = new HashMap();
		map.put(Constant.RESPONSE_STATE_KEY, Code.RESPONSE_SUCCESS);
		map.put(Constant.RESPONSE_MSG_KEY, Constant.SUCCESS);
		return getJsonObject(data, map,true);
	}

	public static JSONObject successJson(String msg) {

		Map map = new HashMap();
		map.put(Constant.RESPONSE_STATE_KEY, Code.RESPONSE_SUCCESS);
		map.put(Constant.RESPONSE_MSG_KEY, msg);
		return getJsonObject(null, map,true);
	}

	public static JSONObject successJson(Object data, String msg) {

		Map map = new HashMap();
		map.put(Constant.RESPONSE_STATE_KEY, Code.RESPONSE_SUCCESS);
		map.put(Constant.RESPONSE_MSG_KEY,msg);
		return getJsonObject(data, map,true);
	}

	public static JSONObject successJsonVesion(Object data, String msg) {

		Map map = new HashMap();
		map.put(Constant.RESPONSE_STATE_KEY, Code.RESPONSE_SUCCESS);
		map.put(Constant.RESPONSE_MSG_KEY,msg);
		return getJsonObject(data, map,true);
	}


	/**
	 * 成功的JSON
	 * @param resultFlag 返回结果
	 * @return
	 */
	public static JSONObject successJsons(Map resultFlag) {
		resultFlag.put("result","2");
		Map map = new HashMap();


		map.put(Constant.RESPONSE_STATE_KEY, Constant.SUCCESS);
		map.put(Constant.RESPONSE_MSG_KEY, Constant.SUCCESS);

		return getJsonObject(resultFlag, map,true);
	}


	/**
	 * 错误的JSON
	 * @param msg 提示信息
	 * @return
	 */
	public static JSONObject errorJson(String msg) {
		Map map = new HashMap();
		map.put(Constant.RESPONSE_STATE_KEY, Code.RESPONSE_FAIL);
		map.put(Constant.RESPONSE_MSG_KEY, msg);

		return getJsonObject(null, map,true);
	}

	/**
	 * 错误的JSON
	 * @param code 错误码
	 * @param msg 提示信息
	 * @return
	 */
	public static JSONObject errorJson(int code, String msg) {
		Map map = new HashMap();
		map.put(Constant.RESPONSE_STATE_KEY, code);
		map.put(Constant.RESPONSE_MSG_KEY, msg);

		return getJsonObject(null, map,true);
	}

	/**
	 * 错误的JSON
	 * @return
	 */
	public static JSONObject invalidTokenJson() {
		Map map = new HashMap();

		map.put(Constant.RESPONSE_STATE_KEY, Code.RESPONSE_INVALID_TOKEN);
		map.put(Constant.RESPONSE_MSG_KEY, "您的登录信息失效，请重新登录");

		return getJsonObject(null, map,true);
	}

	/**
	 * 错误的JSON
	 * @param msg 提示信息
	 * @return
	 */
	public static JSONObject invalidParamJson(String msg) {
		Map map = new HashMap();

		map.put(Constant.RESPONSE_STATE_KEY, Code.RESPONSE_INVALID_PARAM);
		map.put(Constant.RESPONSE_MSG_KEY, msg);

		return getJsonObject(null, map,true);
	}

	/**
	 * 安卓端：逻辑错误但是需要进行页面跳转，需要用success标识
	 * @param resultFlag 包含逻辑错误的提示
	 * @return
	 */
	public static JSONObject successJsonSetResult(Map resultFlag) {
		Map map = new HashMap();

		map.put(Constant.RESPONSE_STATE_KEY, Code.RESPONSE_INVALID_PARAM);
		map.put(Constant.RESPONSE_MSG_KEY, Constant.SUCCESS);

		return getJsonObject(resultFlag, map,true);
	}


	private static JSONObject getJsonObject(Object data, Map map, boolean flag) {

		// 测试环境返回结果显示未加密数据
		if (devFlag) {
			map.put(Constant.RESPONSE_DEV_RESULT_KEY, data);
		}

		if (data == null) {
			map.put(Constant.RESPONSE_RESULT_KEY, null);
		} else if (!flag) {
			map.put(Constant.RESPONSE_RESULT_KEY, data);
		} else {
			if (data.getClass() == String.class) {
				map.put(Constant.RESPONSE_RESULT_KEY, AesUtils.encrypt_ecb(data.toString()));
			} else {
				map.put(Constant.RESPONSE_RESULT_KEY, AesUtils.encrypt_ecb(JSON.toJSONStringWithDateFormat(data,"yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteDateUseDateFormat)));
			}
		}
		JSONObject jsonObject = (JSONObject) JSONObject.toJSON(map);
		return jsonObject;
	}
}
